﻿//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2011 , DotNet, Ltd.
//-------------------------------------------------------------------------------------

using System;
using System.Data;
using System.Reflection;

namespace DotNet.Service
{
    using DotNet.Business;
    using DotNet.DbUtilities;
    using DotNet.IService;
    using DotNet.Model;
    using DotNet.Utilities;

    /// <summary>
    /// ExceptionService
    /// 异常记录服务
    ///
    /// 修改纪录
    ///
    ///     2007.06.12 版本：1.1 DotNet 加入调试信息#if (DEBUG)。
    ///		2007.06.07 版本：1.0 DotNet 窗体与数据库连接的分离。
    ///
    /// 版本：1.1
    ///
    /// <author>
    ///		<name>DotNet</name>
    ///		<date>2007.06.12</date>
    /// </author>
    /// </summary>
    public class ExceptionService : System.MarshalByRefObject, IExceptionService
    {
        /// <summary>
        /// 用户中心数据库连接
        /// </summary>
        private readonly string UserCenterDbConnection = BaseSystemInfo.UserCenterDbConnection;

        #region public DataTable GetDT(BaseUserInfo userInfo) 窗体加载

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetDT(BaseUserInfo userInfo)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
            DataTable dataTable = new DataTable(BaseExceptionTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseExceptionManager Exception = new BaseExceptionManager(dbHelper, userInfo);
                dataTable = Exception.GetDataTable();
                dataTable.TableName = BaseExceptionTable.TableName;
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
#if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
            return dataTable;
        }

        #endregion public DataTable GetDT(BaseUserInfo userInfo) 窗体加载

        #region public DataTable Delete(BaseUserInfo userInfo, string id) 批量删除日志

        /// <summary>
        /// 批量删除异常
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>数据表</returns>
        public DataTable Delete(BaseUserInfo userInfo, string id)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
            DataTable dataTable = new DataTable(BaseExceptionTable.TableName);

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseExceptionManager exceptionManager = new BaseExceptionManager(dbHelper, userInfo);
                exceptionManager.Delete(id);
                dataTable = exceptionManager.GetDataTable();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
#if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
            return dataTable;
        }

        #endregion public DataTable Delete(BaseUserInfo userInfo, string id) 批量删除日志

        #region public int BatchDelete(BaseUserInfo userInfo, string[] ids) 批量删除异常

        /// <summary>
        /// 批量删除异常
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
        /// <returns>数据表</returns>
        public int BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
            int returnValue = 0;

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseExceptionManager exceptionManager = new BaseExceptionManager(dbHelper, userInfo);
                returnValue = exceptionManager.BatchDelete(BaseExceptionTable.FieldId, ids);
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
#if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
            return returnValue;
        }

        #endregion public int BatchDelete(BaseUserInfo userInfo, string[] ids) 批量删除异常

        #region public int Truncate(BaseUserInfo userInfo) 清除异常

        /// <summary>
        /// 清除异常
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public int Truncate(BaseUserInfo userInfo)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
            int returnValue = 0;

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                BaseExceptionManager exceptionManager = new BaseExceptionManager(dbHelper, userInfo);
                exceptionManager.Truncate();
                BaseLogManager.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception ex)
            {
                BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
#if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
            return returnValue;
        }

        #endregion public int Truncate(BaseUserInfo userInfo) 清除异常


        public string LogException(BaseUserInfo userInfo, Exception exception)
        {
#if (DEBUG)
            int milliStart = BaseBusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif
            string returnValue = "";

            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open(UserCenterDbConnection);
                returnValue = BaseExceptionManager.LogException(dbHelper, userInfo, exception);

            }
            catch (Exception ex)
            {
                returnValue = BaseExceptionManager.LogException(dbHelper, userInfo, ex);
                throw ex;
            }
            finally
            {
                dbHelper.Close();
            }

            // 写入调试信息
#if (DEBUG)
            BaseBusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif
            return returnValue;
        }
    }
}